package com.xgkj.diyiketang.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.umeng.analytics.MobclickAgent;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.BangDanActivity;
import com.xgkj.diyiketang.activity.SearchAnchorActivity;
import com.xgkj.diyiketang.activity.data.XiaoYuanBangAdapter;
import com.xgkj.diyiketang.data.GameInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.XiaoYuanBangTask;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

public class Fragment_XiaoYuan extends BaseFragment  implements PullToRefreshListView.OnRefreshListener,PullToRefreshListView.OnLoadDataListener {

	private Context mContext;
	private PullToRefreshListView mListView;
	private XiaoYuanBangAdapter mAdapter;
	private int currentPage = 1;
	private int size = 10;
	private int paihangbangTaskId = -1;
	private ImageView sousuo;

	@Override
	protected int getContentViewId() {
		return 0;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_xiaoyuan,null);
		mContext = getActivity();
		initView(view);
		return view;
	}
	private void initView(View view){
//		sousuo = (ImageView) view.findViewById(R.id.xiaoyuan_search);
//		sousuo.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View view) {
//				startActivity(new Intent(mContext, SearchAnchorActivity.class));
//			}
//		});
//		mListView = (PullToRefreshListView)view.findViewById(R.id.xiaoyuanbang_list);
		mListView.init(mContext,true);
		mAdapter = new XiaoYuanBangAdapter(mContext);
		mListView.setAdapter(mAdapter, true);

		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if(mListView.getHeaderViewsCount()>0){
					position--;
				}
				if (position<0 || position>=mAdapter.getCount())return;
				GameInfo fdinfo = (GameInfo) mAdapter.getItem(position);
				String fdid = fdinfo.getGameId();
				String state = fdinfo.getGameState();
				Intent intent = new Intent(mContext,BangDanActivity.class);
				intent.putExtra("GAMEID",fdid);
				intent.putExtra("STATE",state);
				startActivity(intent);
			}
		});
		mListView.setOnRefreshListener(this);
		mListView.setOnLoadDataListener(this);
		mListView.doRefresh();
	}

	private void getXiaoYuanBang() {
		if(paihangbangTaskId != -1){
			return;
		}
		if(currentPage == 0){
			mListView.setNoNextPage();
			return;
		}

		XiaoYuanBangTask task = new XiaoYuanBangTask(size, currentPage);
		task.addListener(new XiaoYuanListener(mContext));
		paihangbangTaskId = getEatShowApp().getTaskManager().addTask(task);
	}

	class XiaoYuanListener extends ShowToastTaskEventListener{
		public XiaoYuanListener(Context context) {
			super(context);
		}
		@Override
		public void handleTaskCompleted(int taskId, int result, Object data) {
			super.handleTaskCompleted(taskId, result, data);
			if(taskId == paihangbangTaskId){
				paihangbangTaskId = -1;
				if(currentPage == 1){
					mAdapter.clearData();
					mListView.onRefreshComplete();
					mListView.enableLoadMore();
				}else{
					mListView.onLoadDataComplete(true);
				}
				List<GameInfo> fansList = new ArrayList<GameInfo>();
				if(data != null){
					fansList = (List<GameInfo>) data;
				}

				if(fansList != null && fansList.size()>0){
					mAdapter.updateData(fansList);
				}

				currentPage = result;

			}
		}

		@Override
		public void handleTaskError(int taskId, int errorCode, String errorMsg) {
			super.handleTaskError(taskId, errorCode, errorMsg);
			if(taskId == paihangbangTaskId){
				paihangbangTaskId = -1;
				if(currentPage == 1){
					mListView.onRefreshComplete();
				}else{
					mListView.onLoadDataComplete(false);
				}
			}
		}

		@Override
		public void handleTaskMessage(int taskId, Object data) {
			super.handleTaskMessage(taskId, data);
		}

	}
	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		if(!hidden){
			//mListView.invalidate();
			if(mAdapter != null && mAdapter.getCount() == 0){
				mListView.doRefresh();
			}

		}
	}

	@Override
	public void onPullRefresh() {

	}

	@Override
	public void onLoadMore() {
		getXiaoYuanBang();
	}

	@Override
	public void onRefresh() {
		currentPage=1;
		getXiaoYuanBang();
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd("XiaoYuanFragment");
	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart("XiaoYuanFragment");
		mListView.doRefresh();
	}
}
